package com.cgtong.android.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.MessageBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.GetMessageContract;
import com.cgtong.android.mvp.presenter.GetMessagePresenter;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.BadgeView;
import com.kingja.loadsir.callback.SuccessCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 我的消息
 */
public class MyMessageActivity extends BaseTitleActivity implements GetMessageContract.View {

    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.bg_notice)
    BadgeView bg_notice;
    @BindView(R.id.bg_inform)
    BadgeView bg_inform;
    @BindView(R.id.bg_recept)
    BadgeView bg_recept;
    @BindView(R.id.bg_inter)
    BadgeView bg_inter;
    @BindView(R.id.layout_notice)
    LinearLayout layout_notice;
    @BindView(R.id.txt_notice)
    TextView txt_notice;
    @BindView(R.id.layout_inform)
    LinearLayout layout_inform;
    @BindView(R.id.txt_inform)
    TextView txt_inform;
    @BindView(R.id.layout_recept)
    LinearLayout layout_recept;
    @BindView(R.id.txt_recept)
    TextView txt_recept;
    @BindView(R.id.layout_inter)
    LinearLayout layout_inter;
    @BindView(R.id.txt_inter)
    TextView txt_inter;

    @BindView(R.id.txt_notice_time)
    TextView txt_notice_time;
    @BindView(R.id.txt_inform_time)
    TextView txt_inform_time;
    @BindView(R.id.txt_recept_time)
    TextView txt_recept_time;
    @BindView(R.id.txt_inter_time)
    TextView txt_inter_time;

    private GetMessagePresenter messagePresenter;

    @Override
    protected String getContentTitle() {
        return getString(R.string.title_center_my_msg);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_message;
    }

    @Override
    protected void loadDataByNet() {
        initView();
    }

    @Override
    protected void onDataReloadByNet(View v) {

    }

    private void initView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMsgData();
            }
        });
        messagePresenter = new GetMessagePresenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMsgData();
    }

    private void getMsgData() {
        messagePresenter.getUnreadMsg();
    }

    @OnClick({R.id.title_left_txt, R.id.layout_notice, R.id.layout_inform, R.id.layout_recept, R.id.layout_inter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_txt:
                finish();
                break;
            case R.id.layout_notice://系统公告
                MyMessageListActivity.start2Activity(this, getString(R.string.title_msg_notice), ConstantManager.MsgType.NOTICE);
                break;
            case R.id.layout_inform://活动通知
                MyMessageListActivity.start2Activity(this, getString(R.string.title_msg_inform), ConstantManager.MsgType.INFORM);
                break;
            case R.id.layout_recept://问题受理通知
                MyMessageListActivity.start2Activity(this, getString(R.string.title_msg_recept), ConstantManager.MsgType.RECEPT);
                break;
            case R.id.layout_inter://互动通知
                MyMessageListActivity.start2Activity(this, getString(R.string.title_msg_inter), ConstantManager.MsgType.INTER);
                break;
            default:
                break;
        }
    }

    @Override
    public void setDatas(List<MessageBean> list) {
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
//        try {
            if (list != null && list.size() > 0) {
                for (MessageBean msg : list) {
//                    long msgId = msg.getId();
                    int msgtype = msg.getType();
                    //"msgNum":"99+","0"
//                    int msgNum = TextUtils.isEmpty(msg.getMsgNum()) ? 0 : Integer.valueOf(msg.getMsgNum());
                    String msgNum = msg.getMsgNum();
                    String msgContent = msg.getContent();
                    String time = msg.getMsgTime();
                    boolean isShow = !"0".equals(msgNum);
                    switch (msgtype) {
                        case ConstantManager.MsgType.NOTICE:
                            bg_notice.setBadgeVisible(isShow);
                            bg_notice.setBadgeText(msgNum);
                            txt_notice.setText(msgContent);
                            txt_notice_time.setText(time);
                            break;
                        case ConstantManager.MsgType.INFORM:
                            bg_inform.setBadgeVisible(isShow);
                            bg_inform.setBadgeText(msgNum);
                            txt_inform.setText(msgContent);
                            txt_inform_time.setText(time);
                            break;
                        case ConstantManager.MsgType.RECEPT:
                            bg_recept.setBadgeVisible(isShow);
                            bg_recept.setBadgeText(msgNum);
                            txt_recept.setText(msgContent);
                            txt_recept_time.setText(time);
                            break;
                        case ConstantManager.MsgType.INTER:
                            bg_inter.setBadgeVisible(isShow);
                            bg_inter.setBadgeText(msgNum);
                            txt_inter.setText(msgContent);
                            txt_inter_time.setText(time);
                            break;
                        default:
                            break;
                    }
                }
            }
//        } catch (NumberFormatException e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public void setNullData() {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }
}
